/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.admin;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.core.template.TemplateManager;
import io.jpress.model.Permission;
import io.jpress.model.PermissionGroup;
import io.jpress.model.base.BasePermission;
import io.jpress.model.base.BasePermissionGroup;
import io.jpress.permission.PermissionTypes;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.service.PermissionGroupService;
import io.jpress.service.PermissionService;
import io.jpress.web.AdminPermissionGroups;
import io.jpress.web.base.AdminControllerBase;
import io.jpress.web.permission.PermissionUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Title: 权限相关 Controller
 */
@RequestMapping(value = "/admin/permission", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _PermissionController extends AdminControllerBase {

    @Inject
    private PermissionService permissionService;

    @Inject
    private PermissionGroupService permissionGroupService;


    /**
     * 查看所有权限信息
     */
    @PermissionDef(title = "查看权限列表", groupId = AdminPermissionGroups.USER)
    @AdminMenu(text = "权限", groupId = JPressConsts.SYSTEM_MENU_USER, order = 10)
    public void list() {
        Integer type = getParaToInt("type", PermissionTypes.MENU);

        List<Permission> permissions = permissionService.findListByType(type);
        List<PermissionGroup> permissionGroups = permissionGroupService.findListByType(type);

        if (permissionGroups != null && permissions != null) {

            permissions.sort(Comparator.comparing(BasePermission::getId));
            permissionGroups.sort(Comparator.comparingInt(BasePermissionGroup::getSortNo));

            for (Permission permission : permissions) {
                for (PermissionGroup permissionGroup : permissionGroups) {
                    if (Objects.equals(permission.getGroupId(), permissionGroup.getId())) {
                        permissionGroup.addPermission(permission);
                    }
                }
            }
        }

        setAttr("groups", permissionGroups);
        setAttr("type", type);

        render("user/permission.html");
    }


    /**
     * 同步系统权限
     */
    @PermissionDef(title = "同步系统权限", groupId = AdminPermissionGroups.USER)
    public void syncPermissions() {

        List<Permission> allPermissions = new ArrayList<>();
        allPermissions.addAll(PermissionUtil.buildActionPermissions());
        allPermissions.addAll(PermissionUtil.buildMenuPermissions());
        allPermissions.addAll(PermissionUtil.buildMaintainerPermissions());
        permissionService.sync(allPermissions);


        List<PermissionGroup> allPermissionGroups = new ArrayList<>();
        allPermissionGroups.addAll(PermissionUtil.buildMaintainerPermissionGroups());
        allPermissionGroups.addAll(PermissionUtil.buildMenuPermissionGroups());
        permissionGroupService.sync(allPermissionGroups);

        PermissionGroup permissionGroup = TemplateManager.me().getCurrentTemplate().getPermissionGroup();
        permissionGroupService.syncGroupAndPemissions(permissionGroup);


        renderJson(Ret.ok("message", "权限信息同步成功！"));
    }

}
